var app = angular.module('ZJPC');
app.controller('FellowCarController', ['$scope','$state','$rootScope','$stateParams','$filter','DataService',FellowCarController]);

function FellowCarController($scope,$state,$rootScope,$stateParams,$filter,DataService) {

	$scope.$on('detaildata',function(e,data){
		$scope.fcar = data.fellowcar;
		$scope.starttime = data.starttime;
		$scope.trackName = data.xm;
	})

	if($stateParams.datag!=null){ 
		$scope.fcar = $stateParams.datag.fellowcar;
		$scope.starttime = $stateParams.datag.starttime;
		$scope.trackName = $stateParams.datag.xm;
	}

	$scope.chosen = 0;
	var starttime = new Date($scope.starttime).setHours(0,0,0)
	var endtime = new Date($scope.starttime).setHours(23,59,59)

	if($scope.fcar!=null&&$scope.fcar.length!=0){
		$scope.loading = true;
		DataService.getCarTrack($filter('date')(starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')(endtime,'yyyy-MM-dd HH:mm:ss'),$scope.fcar[0].cph,function(data){
			var dataArray = []
			console.log(data)
			if(data.length>0){
				for(var i =0;i<data.length;i++){
					dataArray.push({time:data[i].passTime,track:[data[i].camerJd,data[i].camerWd]})
				}
				CesiumService.getTrack(dataArray,'fellow')
			}
			$scope.loading = false;
		})	
	}


	//点选控制
	$scope.check = function(index){
		if(index!=$scope.chosen){
			$scope.chosen = index;
		}
		if($scope.chosenTrack==0){
			//本轨迹
			//hide同行轨迹点
			//show本轨迹点
		}else{
			//show同行轨迹点
			//hide本轨迹点
		}
	}

	$scope.chosenTrack = 0;

	$scope.checkTrack = function(i){
		$scope.chosenTrack = i;
		//0的情况,代表选中本轨迹
		if(i==0){
			//hide同行轨迹点
			//show本轨迹
		}else{
			//show同行轨迹点
			//hide本轨迹点
		}
	}

	$scope.$watch('chosen',function(newValue,oldValue){
		if(newValue!=oldValue){
			$scope.loading = true;
			DataService.getCarTrack($filter('date')(starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')(endtime,'yyyy-MM-dd HH:mm:ss'),$scope.fcar[newValue].cph,function(data){
				$scope.loading = false;
				var dataArray = []
				console.log(data)
				if(data.length>0){
					for(var i =0;i<data.length;i++){
						dataArray.push({time:data[i].passTime,track:[data[i].camerJd,data[i].camerWd]})
					}
					CesiumService.getTrack(dataArray,'fellow')
				}
			})	
		}
	})

	$scope.$on("$destroy", function() {
        //这里清除fellowcar的轨迹与红点位
    })

}